package com.cyy.dao;

import com.cyy.bean.salesman;
import com.cyy.bean.user;
import com.cyy.util.dbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class salesmanDao {
    public List<salesman> findAll() throws ClassNotFoundException, SQLException {
        List<salesman> salesmanList = new ArrayList<>();
        Connection connection = dbUtil.getConnection();
        System.out.println("获得连接成功");
        String sql = "select * from salesman ";
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();
        salesman salesman = null;
        String salesmanname,password,email,registerTime;
        int id;
        while (resultSet.next()) {
            id=resultSet.getInt("salesmanId");
            salesmanname = resultSet.getString("name");
            password = resultSet.getString("pwd");
            email = resultSet.getString("email");
            registerTime=resultSet.getString("registerTime");
            salesman=new salesman(id,salesmanname,password,email,registerTime);
            salesmanList.add(salesman);
        }
        dbUtil.close(resultSet, statement, connection);
        System.out.println(salesmanList);
        return salesmanList;
    }


    public boolean selectNamePwd(String name,String pwd) throws SQLException, ClassNotFoundException {
        Connection conn = dbUtil.getConnection();
        String sql="select * from salesman where name=? and pwd=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,name);
        pstmt.setString(2,pwd);
        ResultSet resultSet = pstmt.executeQuery();
        if(resultSet.next())
        {
            if(resultSet.getString("name")==null||resultSet.getString("pwd")==null)
            {
                dbUtil.close(resultSet, pstmt, conn);
                return false;
            }
            else
            {
                dbUtil.close(resultSet, pstmt, conn);
                return true;
            }
        }
        return false;
    }
    public String selectEmail(String name) throws SQLException, ClassNotFoundException {
        Connection conn=dbUtil.getConnection();
        String sql="select email from salesman where name=?";
        PreparedStatement pstmt =conn.prepareStatement(sql);
        pstmt.setString(1,name);
        ResultSet resultSet = pstmt.executeQuery();
        if(resultSet.next()) {
            return resultSet.getString("email");
        }
        return "";
    }
    public String selectPwd(String name) throws SQLException, ClassNotFoundException {
        Connection conn=dbUtil.getConnection();
        String sql="select pwd from salesman where name=?";
        PreparedStatement pstmt =conn.prepareStatement(sql);
        pstmt.setString(1,name);
        ResultSet resultSet = pstmt.executeQuery();
        if(resultSet.next()) {
            return resultSet.getString("pwd");
        }
        return "";
    }
    public String selectRegisterTime(String name) throws SQLException, ClassNotFoundException {
        Connection conn=dbUtil.getConnection();
        String sql="select registerTime from salesman where name=?";
        PreparedStatement pstmt =conn.prepareStatement(sql);
        pstmt.setString(1,name);
        ResultSet resultSet = pstmt.executeQuery();
        if(resultSet.next()) {
            return resultSet.getString("registerTime");
        }
        return "";
    }

    public salesman selectAll(String userName) throws SQLException, ClassNotFoundException {
        Connection conn=dbUtil.getConnection();
        String sql="select * from salesman where name=?";
        PreparedStatement pstmt =conn.prepareStatement(sql);
        pstmt.setString(1,userName);
        ResultSet resultSet = pstmt.executeQuery();
        if(resultSet.next()) {
            String registerTime=resultSet.getString("registerTime");
            String pwd=resultSet.getString("pwd");
            String email=resultSet.getString("email");
            return new salesman(0,userName,pwd,email,registerTime);
        }
        return null;
    }
}
